package com.yao2san.sim.storage.client.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JSONUtil {
    public static final ObjectMapper MAPPER;

    static {
        MAPPER = new ObjectMapper();
        MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    private JSONUtil() {
    }

    public static String toJSONString(Object object) {
        try {
            return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("转换Json出错", e);
        }
    }

    public static <T> T parseObject(String content, Class<T> type) {
        try {
            return MAPPER.readValue(content, type);
        } catch (IOException e) {
            throw new RuntimeException("JSON转换异常", e);
        }
    }

}
